package com.itheima.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;



@RequestMapping("/employee")
@RestController
@Api(tags = "员工管理")
public class EmployeeController  {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    @ApiOperation(value = "员工登录方法")
    public R<Employee> login(@RequestBody Employee e, HttpSession session){
        QueryWrapper<Employee> qw = new QueryWrapper<>();
        qw.eq("username",e.getUsername());
        Employee one = employeeService.getOne(qw);
        if (one == null ){
            return R.error("用户不存在...");
        }
        String password = e.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!md5Password.equals(one.getPassword())){
            return R.error("密码错误...");
        }
        if (one.getStatus()==0){
            return R.error("状态禁用!");
        }
        session.setAttribute("employee",one.getId());
        return R.success(one);
    }

    @ApiOperation(value = "员工登出方法")
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功...");

    }

    @ApiOperation(value = "员工添加方法")
    @PostMapping
    public R add(@RequestBody Employee e,HttpSession session){
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,e.getUsername());
        Employee one = employeeService.getOne(lqw);
        if (one != null){
            return R.error("用户名已存在!");
        }
        e.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        e.setCreateTime(LocalDateTime.now());
//        e.setUpdateTime(LocalDateTime.now());
//        long ele = (long) session.getAttribute("employee");
//        e.setUpdateUser(ele);
//        e.setCreateUser(ele);
        employeeService.save(e);
        return R.success("添加成功");
    }

    @ApiOperation(value = "员工分页查询方法")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name="page",value = "页码",required = true),
            @ApiImplicitParam(name="pageSize",value = "当前页数据",required = true),
            @ApiImplicitParam(name="name",value = "查询条件",required = false),
    })
    public R page(Integer page,Integer pageSize,String name){
        IPage<Employee> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            lqw.like(Employee::getName,name);
        }
        IPage<Employee> iPage = employeeService.page(pageInfo,lqw);
        return R.success(iPage);
    }

    @PutMapping
    @ApiOperation(value = "员工修改方法")
    public R update(@RequestBody Employee e,HttpSession session){
//        long employee = (long) session.getAttribute("employee");
//        e.setUpdateUser(employee);
//        e.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(e);
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询员工方法")
    public R<Employee> getById(@PathVariable long id){
        Employee e = employeeService.getById(id);
        if (e!=null){
            return R.success(e);
        };
        return R.error("没有查询到!");
    }
}
